package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplus.mapper.BuildingMapper;
import com.example.mybatisplus.model.domain.Building;
import com.example.mybatisplus.model.domain.SuperAdmin;
import com.example.mybatisplus.mapper.SuperAdminMapper;
import com.example.mybatisplus.service.SuperAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2024-06-24
 */
@Service
public class SuperAdminServiceImpl extends ServiceImpl<SuperAdminMapper, SuperAdmin> implements SuperAdminService {
    @Autowired
    private SuperAdminMapper superAdminMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    @Transactional
    public Long buildingAdd(Map<String, Object> params) {

        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        List<Building> buildings = buildingMapper.selectList(wrapper);
        if(buildings.size()!=0){
            for(Building building:buildings){
                if(building.getIsDeleted()==0){
                    return -1L;
                }
                else{
                    building.setIsDeleted(0);
                    buildingMapper.updateById(building);
                    return building.getId();
                }
            }
        }

        Building building = new Building()
                .setIsDeleted(0)
                .setModifiedTime(LocalDateTime.now())
                .setCreatedTime(LocalDateTime.now())
                .setArea(Long.parseLong(params.get("area").toString()))
                .setMaxFloor((int)params.get("max_floor"))
                .setType((String)params.get("type"))
                .setName((String)params.get("name"));
        buildingMapper.insert(building);
        return building.getId();

    }
}
